package br.inf.solutions.apresentacoes.ssp.control.validator;

import java.lang.reflect.Field;
import java.util.Date;
import java.util.List;
import java.util.Map;

import br.com.vexillum.util.CPFUtils;
import br.com.vexillum.util.DateCalc;
import br.com.vexillum.util.ReflectionUtils;
import br.com.vexillum.util.Return;
import br.com.vexillum.util.SpringFactory;
import br.inf.solutions.apresentacoes.ssp.control.PersonController;
import br.inf.solutions.apresentacoes.ssp.model.Person;
import br.inf.solutions.apresentacoes.ssp.model.annotations.FifthStep;
import br.inf.solutions.apresentacoes.ssp.model.annotations.FirstStep;
import br.inf.solutions.apresentacoes.ssp.model.annotations.FourthStep;
import br.inf.solutions.apresentacoes.ssp.model.annotations.SecondStep;
import br.inf.solutions.apresentacoes.ssp.model.annotations.ThirdStep;

public class PersonValidator extends BaseValidator {

	public PersonValidator(Map<String, Object> mapData) {
		super(mapData);
	}
	
	public Return validateGetPersonByCPF(){
		Return ret = new Return(true);
		ret.concat(validateAttribute("cpf"));
		ret.concat(validateCPF());
		return ret;
	}
	
	public Return validateResearchPersonFunction(){
		Return ret = new Return(true);
		ret.concat(validateAttribute("unity"));
		return ret;
	}
	
	private Return validateCPF(){
		Return ret = new Return(true);
		Person person = (Person) entity;
		if(!CPFUtils.validateCPF(person.getCpf())){
			ret.concat(creatReturn("cpf", getValidationMessage(null, "validcpf", false)));
		}
		return ret;
	}
	
	private Return validateMajority(Date date){
		Return ret = new Return(true);
		if(DateCalc.calculateAge(date) < 18){
			return new Return(false);
		}
		return ret;
	}
	
	@SuppressWarnings("unused")
	private Return validateDateInclusion(){
		Return ret = new Return(true);
		if(!validateMajority(((Person) entity).getDateInclusion()).isValid()){
			ret.concat(creatReturn("dateInclusion", getValidationMessage(null, "majority", false)));
		}
		return ret;
	}
	
	private Return validateBirthDate(){
		Return ret = new Return(true);
		if(!validateMajority(((Person) entity).getBirthDate()).isValid()){
			ret.concat(creatReturn("birthDate", getValidationMessage(null, "majority", false)));
		}
		return ret;
	}
	
	private Return validateUniqueRG(){
		Return ret = new Return(true);
		Person person = (Person) entity;
		PersonController controller = SpringFactory.getController("personController", PersonController.class, null);
		String sql = "FROM Person WHERE rg = '" + person.getRg() + "'";
		controller.getData().put("sql", sql);
		if(person.isNew()){
			if(controller.searchByHQL().getList()!=null && !controller.searchByHQL().getList().isEmpty() && controller.searchByHQL().getList().get(0) != null){
				ret.concat(creatReturn("rg", getValidationMessage(null, "existsrg", true)));
			}
		}
		return ret;
		
	}
	
	public Return validateFirstStep(){
		Return ret = new Return(true);
		List<Field> list = ReflectionUtils.getAnnotatedFields(entity, FirstStep.class);
		for(Field f : list){
			ret.concat(validateAttribute(f));
		}
		ret.concat(validateUniqueRG());
		ret.concat(validateBirthDate());
		return ret;
	}
	
	public Return validateSecondStep(){
		Return ret = new Return(true);
		List<Field> list = ReflectionUtils.getAnnotatedFields(entity, SecondStep.class);
		for(Field f : list){
			ret.concat(validateAttribute(f));
		}
		
		return ret;
	}
	
	public Return validateThirdStep(){
		Return ret = new Return(true);
		List<Field> list = ReflectionUtils.getAnnotatedFields(entity, ThirdStep.class);
		for(Field f : list){
			ret.concat(validateAttribute(f));
		}
		return ret;
	}
	
	public Return validateFourthStep(){
		Return ret = new Return(true);
		List<Field> list = ReflectionUtils.getAnnotatedFields(entity, FourthStep.class);
		for(Field f : list){
			ret.concat(validateAttribute(f));
		}
		return ret;
	}
	
	public Return validateFifthStep(){
		Return ret = new Return(true);
		List<Field> list = ReflectionUtils.getAnnotatedFields(entity, FifthStep.class);
		for(Field f : list){
			ret.concat(validateAttribute(f));
		}
		return ret;
	}
}
